import wepy from 'wepy'
let QQMapWX = require('../libs/qqmap-wx-jssdk.min.js');
let qqmapsdk = new QQMapWX({ key: '7EVBZ-6AAKW-ALYRY-OTQ3P-T6DIF-CEF2Q' });

export default class SetMarker extends wepy.mixin {
  data = {
  }

  setdata(datasource){
    let self=this
    if(datasource.lng>0){
      let newPoint = this.wgs84togcj02(datasource.lng, datasource.lat);
      this.latitude=newPoint[1].toFixed(6)
      this.longitude=newPoint[0].toFixed(6)
      this.trackerId=datasource.trackerId
      let time = new Date(), rssi = '',satellites = '';
      datasource.rssi <= 10 ? rssi = '弱' : datasource.rssi <= 20 ? rssi = '一般' : rssi = '强';
      datasource.satellites <= 6 ? satellites = '弱' : datasource.satellites <= 14 ? satellites = '一般' : satellites = '强';
      if (datasource.updateTime && datasource.updateTime>0) time = new Date(datasource.updateTime);

      qqmapsdk.reverseGeocoder({
        location: {
          latitude: Number(newPoint[1]).toFixed(6),
          longitude: Number(newPoint[0]).toFixed(6)
        },
        success: function (e) {
          if (e.status === 0) {
            self.setaddrdata(newPoint[1], newPoint[0], datasource.number || '', e.result.address, self.formatTime(time), rssi, satellites)
          }
        },
        fail: function (data) {
          self.setaddrdata(newPoint[1], newPoint[0], datasource.number || '', '', self.formatTime(time), rssi, satellites)
        }
      });
    }
  }

  setaddrdata(latitude, longitude, bike, name, time, rssi, satellites){
    this.markers = [{
      latitude: latitude,
      longitude: longitude,
      callout: {
        content:`车牌号:${bike || ''}\n通信信号:${rssi}\nGPS信号:${satellites}\n经度:${Number(longitude).toFixed(6)}\n纬度:${Number(latitude).toFixed(6)}\n位置:${name}\n定位时间：${time}`,
        display: 'ALWAYS',
        bgColor: '#00000080',
        color: '#ffffff',
        textAlign: 'left',
        borderRadius: 15,
        padding: 15,
        fontSize: 15
      }
    }]
    this.$apply()
  }

}
